package com.xktpx.controller;

import com.xktpx.common.exception.RRException;
import com.xktpx.common.utils.R;
import com.xktpx.entity.SysOssEntity;
import com.xktpx.form.LoginForm;
import com.xktpx.msg.ICaptchaApi;
import com.xktpx.msg.model.MsgReq;
import com.xktpx.oss.OSSFactory;
import com.xktpx.result.BaseResult;
import com.xktpx.service.SysOssService;
import com.xktpx.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

/**
 * @author 杨浩
 * @create 2018-12-30 14:57
 **/
@RestController
@RequestMapping("/api/publicInformation")
@Api(tags = "公用信息")
public class ApiPublicInformationController {

    @Autowired
    private ICaptchaApi iCaptchaApi;

    @Autowired
    private UserService userService;
    @Autowired
    private SysOssService sysOssService;

    @ApiOperation("文件上传")
    @PostMapping("/uploadFiles")
    public BaseResult<SysOssEntity> uploadFiles(
            @ApiParam("文件") @RequestParam(value = "file", required = false) MultipartFile file,
            @ApiParam("文件地址") @RequestParam(value = "url", required = false) String url) throws Exception {
        Optional<MultipartFile> fileOptional = Optional.ofNullable(file);
        if (!fileOptional.isPresent() && "".equals(url)) {
            throw new RRException("上传文件不能为空");
        }

        //上传文件
        if (fileOptional.isPresent()) {
            String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
            url = OSSFactory.build().uploadSuffix(file.getBytes(), suffix);
        }

        //保存文件信息
        SysOssEntity ossEntity = new SysOssEntity();
        ossEntity.setUrl(url);
        sysOssService.insert(ossEntity);

        return BaseResult.success(ossEntity);
    }
    @ApiOperation("调用实名认证接口")
    @GetMapping("/authName")
    public R authName(@ApiParam("手机号码") @RequestParam("mobileNo") String mobileNo
            ,@ApiParam("证件号码") @RequestParam("idCardNo") String idCardNo
            ,@ApiParam("姓名") @RequestParam("realName") String realName) {
        //表单校验


        //用户登录
        return R.ok();
    }

    @ApiOperation("查询当前求购商品列表")
    @GetMapping("/queryPurchasesGoodsList")
    public R queryPurchasesGoodsList(@ApiParam("商品ID") @RequestParam("goodsId") Integer goodsId) {
        //表单校验


        //用户登录
        return R.ok();
    }

    @ApiOperation("查询当前出售商品列表")
    @GetMapping("/querySellGoodsList")
    public R querySellGoodsList(@ApiParam("商品ID") @RequestParam("goodsId") Integer goodsId) {
        //表单校验


        //用户登录
        return R.ok();
    }

    @ApiOperation("查询最近成交记录")
    @GetMapping("/transactionRecordList")
    public R transactionRecordList(@ApiParam("商品ID") @RequestParam("goodsId") Integer goodsId) {
        //表单校验


        //用户登录
        return R.ok();
    }
}